global
  log /dev/log  local0
  log /dev/log  local1 notice
{% if haproxy_socket != '' %}
  stats socket {{ haproxy_socket }} level admin
{% endif %}
{% if haproxy_chroot != '' %}
  chroot {{ haproxy_chroot }}
{% endif %}
  daemon
  user {{ haproxy_user }}
  group {{ haproxy_user }}
  maxconn {{ haproxy_maxconn }}
  ulimit-n {{ haproxy_ulimit }}
  nbproc {{ haproxy_nbproc }}
{% for global_var in haproxy_global_vars %}
  {{ global_var }}
{% endfor %}

defaults
  log global
  mode  http
  option  httplog
  option  dontlognull
{% if haproxy_version == '1.4' %}
  contimeout 5000
  clitimeout 50000
  srvtimeout 50000
{% else %}
  timeout connect 5000
  timeout client 50000
  timeout server 50000
{% endif %}
  errorfile 400 /usr/local/haproxy/errorfiles/400.http
  errorfile 403 /usr/local/haproxy/errorfiles/403.http
  errorfile 408 /usr/local/haproxy/errorfiles/408.http
  errorfile 500 /usr/local/haproxy/errorfiles/500.http
  errorfile 502 /usr/local/haproxy/errorfiles/502.http
  errorfile 503 /usr/local/haproxy/errorfiles/503.http
  errorfile 504 /usr/local/haproxy/errorfiles/504.http
{% for defaults_var in haproxy_defaults_vars %}
  {{ defaults_var }}
{% endfor %}

frontend {{ haproxy_frontend_name }}
  bind {{ haproxy_frontend_bind_address }}:{{ haproxy_frontend_port }}
  mode {{ haproxy_frontend_mode }}
  default_backend {{ haproxy_backend_name }}
{% for frontend_var in haproxy_frontend_vars %}
  {{ frontend_var }}
{% endfor %}

backend {{ haproxy_backend_name }}
  mode {{ haproxy_backend_mode }}
  balance {{ haproxy_backend_balance_method }}
  option forwardfor
{% if haproxy_backend_httpchk != '' and haproxy_backend_mode == "http" %}
  option httpchk {{ haproxy_backend_httpchk }}
{% endif %}
{% if haproxy_backend_mode == "http" %}
  cookie SERVERID insert indirect
{% endif %}
{% for backend in haproxy_backend_servers %}
  server {{ backend.name }} {{ backend.address }} {% if haproxy_backend_mode == "http" %} cookie {{ backend.name }} {% endif%}{%if backend.backup is defined %}backup{% endif %} check {% if backend.check is defined %}{{ backend.check }}{% endif %} {% if backend.extra is defined %}{{ backend.extra }}{% endif %} 
{% endfor %}
{% for backend_var in haproxy_backend_vars %}
  {{ backend_var }}
{% endfor %}

{% if haproxy_stats %}
listen statistics
  mode http
  bind *:{{ haproxy_stats_bindport }}
  stats enable
  stats auth {{ haproxy_stats_auth }}
  stats uri {{ haproxy_stats_uri }}
  stats hide-version
  stats admin if TRUE
  stats refresh 3s
  stats show-node
  acl allow src {{ haproxy_stats_allow_src }}
  tcp-request content accept if allow
  tcp-request content reject
  stats realm {{ haproxy_stats_realm }}
{% endif %}

{% if haproxy_conf_extra %}
{{ haproxy_conf_extra }}
{% endif %}
